<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class KcCg extends BaseController
{
//    protected $title='库存采购表';
    protected $title='库存汇总表';//25-07-22改
    protected $table='sale_contract_detail';
    private $sessionKey='pageKcCg';
//    public function index()
//    {
//        $param=$this->param;
//        if (request()->isAjax()) {
//
//            $where[]=['scd.status','=',4];
//            $where[]=['scd.type','=',2];
////            foreach (['bar_code','mdName','specs','brand'] as $i){
////                if (isset($param[$i])&&$param[$i]){
////                    $where[]=[$i,'like',"%".$param[$i].'%'];
////                }
////            }
//            if (isset($param['brand'])&&$param['brand']){
//                $where[]=['md.brand','like',"%".$param['brand'].'%'];
//            }
//            if (isset($param['mdName'])&&$param['mdName']){
//                $where[]=['md.name','like',"%".$param['mdName'].'%'];
//            }
//            if (isset($param['specs'])&&$param['specs']){
//                $where[]=['md.specs','like',"%".$param['specs'].'%'];
//            }
//            if (isset($param['buyCode'])&&$param['buyCode']){
//                $where[]=['md.buyCode','like',"%".$param['buyCode'].'%'];
//            }
//            if (isset($param['taxCode'])&&$param['taxCode']){
//                $where[]=['md.taxCode','like',"%".$param['taxCode'].'%'];
//            }
//            if (isset($param['cgrkd'])&&$param['cgrkd']){
//                $cgMxId=Db::name($this->table)
//                    ->where('type',3)
//                    ->where('status',2)
//                    ->where('stokcOrderNum','like','%'.$param['cgrkd'].'%')
//                    ->column('saleContractDetailId');
//                $xsmxId=Db::name('sale_contract_detail')
//                    ->where('status',4)
//                    ->where('type',2)
//                    ->where('id','in',$cgMxId)
//                    ->column('saleContractDetailId');
//                $where[]=['saleContractDetailId','in',$xsmxId];
//            }
////            halt($where);
//            if (isset($param['cght'])&&$param['cght']){
//                $where[]=['sc.orderNum','like',"%".$param['cght'].'%'];
//            }
//            if (isset($param['xsHt'])&&$param['xsHt']){
//                $where[]=['scd.saleContractOrderNum','like',"%".$param['xsHt'].'%'];
//            }
//
//            if (isset($param['pName'])&&$param['pName']){
//                $where[]=['p.name','like',"%".$param['pName'].'%'];
//            }
//            session($this->sessionKey,$where);
//            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
//            $list = Db::name($this->table)
//                ->alias(' scd')
//                ->leftJoin('sale_contract sc','sc.id = scd.orderId')
//                ->leftJoin('material_dossier md','md.id=scd.mdId')
//                ->leftJoin('purveyor p','p.id=sc.punterId')
//                ->where($where)
//
//                ->field("scd.*,sc.orderNum as orderNum,
//                md.name as mdName, md.brand,md.specs,md.buyCode,md.taxCode,md.unit,p.name as pName")
//                ->order('scd.id DESC')
//                ->paginate($rows)->each(function ($item,$index){
//
//                    $lockNum=Db::name('sale_contract_lock')
//                        ->where('status',1)
//                        ->where('buyContactMxId',$item['id'])
//                        ->sum('lockNum');
//                    $kcNum=Db::name('stock_order_detail')
//                        ->where('status',2)
//                        ->where('type',3)
////                        ->where('saleContractDetailId',$item['id'])
//                        ->where('mdId',$item['mdId'])
//                        ->where('saleContractOrder',$item['orderNum'])
//                        ->sum('num');
//                    $item['lockNum']=$lockNum;
//                    $item['kcNum']=$kcNum;
//                    $canUse=$item['bjNum']-$lockNum;
//                    $kcMoney=$kcNum*$item['bjPrice'];
//                    $item['canUse']=$canUse;
//                    $item['kcMoney']=round($kcMoney,2);
//
//                    return $item;
//                });
//            return table_assign(0, '', $list);
//        } else {
//            $warehouse=Db::name('warehouse')->where('status',1)->field('id,name')->select();
//            View::assign('warehouseList',$warehouse);
//            return view();
//        }
//
//    }

    public function index()
    {
        $param=$this->param;
        if (request()->isAjax()) {

            $where[]=['sod.status','=',2];
            $where[]=['sod.type','in',['3','5']];
            $where[]=['sod.sodStatus','<',3];
//            foreach (['bar_code','mdName','specs','brand'] as $i){
//                if (isset($param[$i])&&$param[$i]){
//                    $where[]=[$i,'like',"%".$param[$i].'%'];
//                }
//            }
            if (isset($param['brand'])&&$param['brand']){
                $where[]=['md.brand','like',"%".$param['brand'].'%'];
            }
            if (isset($param['mdName'])&&$param['mdName']){
                $where[]=['md.name','like',"%".$param['mdName'].'%'];
            }
            if (isset($param['specs'])&&$param['specs']){
                $where[]=['md.specs','like',"%".$param['specs'].'%'];
            }
            if (isset($param['buyCode'])&&$param['buyCode']){
                $where[]=['md.buyCode','like',"%".$param['buyCode'].'%'];
            }
            if (isset($param['taxCode'])&&$param['taxCode']){
                $where[]=['md.taxCode','like',"%".$param['taxCode'].'%'];
            }
            if (isset($param['cgrkd'])&&$param['cgrkd']){
                $cgMxId=Db::name($this->table)
                    ->where('type',3)
                    ->where('status',2)
                    ->where('stokcOrderNum','like','%'.$param['cgrkd'].'%')
                    ->column('saleContractDetailId');
                $xsmxId=Db::name('sale_contract_detail')
                    ->where('status',4)
                    ->where('type',2)
                    ->where('id','in',$cgMxId)
                    ->column('saleContractDetailId');
                $where[]=['saleContractDetailId','in',$xsmxId];
            }
//            halt($where);
            if (isset($param['cght'])&&$param['cght']){
                $where[]=['sc.orderNum','like',"%".$param['cght'].'%'];
            }
            if (isset($param['xsdwName'])&&$param['xsdwName']){
                $where[]=['xsdw.title','like',"%".$param['xsdwName'].'%'];
            }
            if (isset($param['xsHt'])&&$param['xsHt']){
                $where[]=['scd.saleContractOrderNum','like',"%".$param['xsHt'].'%'];
            }

            if (isset($param['pName'])&&$param['pName']){
                $where[]=['p.name','like',"%".$param['pName'].'%'];
            }
            if (isset($param['hsContract'])&&$param['hsContract']){
                $where[]=['sod.hsContract','like',"%".$param['hsContract'].'%'];
            }
            session($this->sessionKey,$where);
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name('stock_order_detail')
                ->alias(' sod')
                ->leftJoin('stock_order so','so.id=sod.orderId' )
                ->leftJoin('material_dossier md','md.id=sod.mdId')
                ->leftJoin('sale_contract_detail scd','scd.id=sod.saleContractDetailId')
                ->leftJoin('sale_contract xsht','xsht.orderNum=scd.saleContractOrderNum and xsht.status>-1')
                ->leftJoin('department xsdw','xsht.salesUnitId=xsdw.id')
                ->leftJoin('sale_contract sc','sc.id=scd.orderId')
                ->leftJoin('purveyor p','p.id=sc.punterId')
                ->where($where)
                ->field("sod.*,so.checkTime,so.orderNum as stockOrderNum,scd.saleContractOrderNum as xsHt,scd.bjPrice ,p.name as pName,
                md.name as mdName, md.brand,md.specs,md.buyCode,md.taxCode,md.unit,scd.remark as cgmxRemark,xsdw.title as xsdwName")
                ->order('sod.id DESC')
                ->paginate($rows)->each(function ($item,$index){
                    $item['outNum']=Db::name('stock_order_detail')
                        ->where('status',2)
                        ->where('thisID','=',$item['id'])
                        ->where('type','in',[1,4,6])->sum('num');
                    $refoundNum=Db::name('stock_order_detail')
                        ->where('status',2)
                        ->where('thisID','=',$item['id'])
                        ->where('type','=',2)->sum('num');
                    $item['bjPrice']=$item['bjPrice']??$item['sodPrice'];
                    $item['hasNum']=$item['num']-$item['outNum']+$refoundNum;
                    $item['kcMoney']=round($item['bjPrice']*$item['hasNum'],2);
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            $warehouse=Db::name('warehouse')->where('status',1)->field('id,name')->select();
            View::assign('warehouseList',$warehouse);
            return view();
        }

    }
    public function seeLock()
    {
        $param=$this->request->param();
        $id=$param['id']??'';
        if ($this->request->post()){
            $info=Db::name($this->table)->where('id',$id)->find();

            if (!$id){
                return table_assign(1,'参数缺失');
            }
            $where[]=['scl.status','>',-1];
            $where[]=['scl.buyContactMxId','=',$id];
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list=Db::name('sale_contract_lock')
                ->alias('scl')
//                ->leftJoin('sale_contract_detail scd','scd.id=scl.detailId')
                ->leftJoin('sale_contract sc','sc.id=scl.orderId')
                ->where($where)
                ->field('scl.*,sc.orderNum')
                ->paginate($rows);
            return table_assign(0,'获取成',$list);

        }else{

            view::assign('id',$id);
            return view('');
        }

    }

    public function cancelLock()
    {
        $id=$this->param['id']??'';
        if (!$id){
            return to_assign(1,'参数缺失');
        }
        $hasLx=Db::name('stock_order_detail')->where('status','>',-1)
            ->where('lockedId',$id)->count();
        if ($hasLx){
            return to_assign(1,'已有关联数据，不可撤回');
        }
        try {
            Db::name('sale_contract_lock')->where('id',$id)->update(['status'=>-1,'deleted'=>time()]);
            $this->param['option']='撤销锁定';
            add_log('back', $id, $this->param,$this->title);
        }catch (\Exception $e){
return to_assign(1,'操作失败：'.$e->getMessage());
        }
        return to_assign();
    }
}